﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using System.IO;
using System.Reflection;
using Langexplr.Abc;
using Microsoft.FSharp.Core;


namespace AbcLibrary.Tests
{
    [TestFixture]
    public class SlotLoadingTests : BaseClassTestFixture
    {
        [Test]
        public void TestBasicLoading()
        {
            using (BinaryReader reader = new BinaryReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("AbcLibrary.Tests.files.slots.abc")))
            {
              //  System.Diagnostics.Debugger.Break();
                AvmClass aClass = LoadClass(reader)[0];
                foreach (AvmMemberSlot slot in aClass.Slots)
                {
                    AvmConstantKindValue initialValue;
                    switch (slot.Name.ToString())
                    {
                        case "something:Data.name":
                            Assert.IsTrue(Option<AvmConstantKindValue>.get_IsSome(slot.InitialValue), "Has initial value");
                            initialValue = slot.InitialValue.Value;
                            Assert.AreEqual(AvmConstantKindValue.tag_OptionalString,
                                            initialValue.Tag);
                            Assert.AreEqual("Fulano", initialValue.OptionalString1);
                            break;
                        case "something:Data.y":
                            Assert.IsTrue(Option<AvmConstantKindValue>.get_IsSome(slot.InitialValue), "Has initial value");
                            initialValue = slot.InitialValue.Value;
                            Assert.AreEqual(AvmConstantKindValue.tag_OptionalInt,
                                            initialValue.Tag);
                            Assert.AreEqual(10, initialValue.OptionalInt1);
                            break;
                        case "something:Data.x":
                            Assert.IsTrue(Option<AvmConstantKindValue>.get_IsSome(slot.InitialValue), "Has initial value");
                            initialValue = slot.InitialValue.Value;
                            Assert.AreEqual(AvmConstantKindValue.tag_OptionalInt,
                                            initialValue.Tag);
                            Assert.AreEqual(10, initialValue.OptionalInt1);
                            break;
                        case "newArraySlot":
                            Assert.IsFalse(Option<AvmConstantKindValue>.get_IsSome(slot.InitialValue), "Has initial value");
                            break;
                        case "PI":
                            Assert.IsTrue(Option<AvmConstantKindValue>.get_IsSome(slot.InitialValue),"Has initial value");
                            initialValue = slot.InitialValue.Value;
                            Assert.AreEqual(AvmConstantKindValue.tag_OptionalDouble,
                                            initialValue.Tag);
                            Assert.AreEqual(3.1415, initialValue.OptionalDouble1);
                            break;
                        default:
                            Assert.IsTrue(false, "Could not identify slot: "+slot.Name);
                            break;
                    }
                }
            }
        }
    }
}
